package at.jku.se.fastsell.gui;

import java.util.List;

import at.jku.se.fastsell.FastsellUI;
import at.jku.se.fastsell.data.Auction;
import at.jku.se.fastsell.data.DataAccess;
import at.jku.se.fastsell.data.User;

import com.vaadin.data.Container;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Component;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Table;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.themes.Reindeer;

public class AuctionsTable extends CustomComponent implements IRefreshable {

	private static final long serialVersionUID = 1L;

	private Container container;
	private AuctionsTableFilter filter;

	// ----------------------------------------------------------------------------------

	public AuctionsTable() {
		Table table = new Table();
		table.setSizeFull();
		table.setStyleName(Reindeer.TABLE_STRONG);

		container = new IndexedContainer();

		container.addContainerProperty("title", String.class, "");
		container.addContainerProperty("description", String.class, "");
		container.addContainerProperty("curPrice", Double.class, 0.0);
		container.addContainerProperty("bidder", String.class, "-");
		container.addContainerProperty("seller", String.class, "");
		container.addContainerProperty("bidCount", Integer.class, 0);
		container.addContainerProperty("remainingTime", String.class, "");
		container.addContainerProperty("details", Component.class, null);

		table.setContainerDataSource(container);

		this.setCompositionRoot(table);
	}

	// ----------------------------------------------------------------------------------

//	@Override
	@SuppressWarnings("unchecked")
	public void refreshData() {
		container.removeAllItems();

		List<Auction> auctions = DataAccess.getAllAuctions();

		for (Auction a : auctions) {
			if (filter != null && filter.filterAuction(a)) {
				continue;
			}
			Object itemId = container.addItem();

			container.getContainerProperty(itemId, "title").setValue(a.getTitle());
			container.getContainerProperty(itemId, "description").setValue(a.getDescription());
			container.getContainerProperty(itemId, "curPrice").setValue(a.getCurPrice());
			
			User bidder = a.getHighestBidder();
			if (bidder != null) {
				container.getContainerProperty(itemId, "bidder").setValue(bidder.getName());
			}
			
			container.getContainerProperty(itemId, "seller").setValue(a.getSeller().toString());
			container.getContainerProperty(itemId, "bidCount").setValue(a.getBidCount());
			container.getContainerProperty(itemId, "remainingTime").setValue(a.getRemainingTime());
			container.getContainerProperty(itemId, "details").setValue(new DetailsButton(a));

		}
	}
	
	/**
	 * Filters table content according to filter
	 * @param filter - filter object, set to null to disable filter
	 */
	public void enableFilter(AuctionsTableFilter filter) {
		this.filter = filter;
		this.refreshData();
	}

	// ----------------------------------------------------------------------------------
	// ----------------------------------------------------------------------------------

	private class DetailsButton extends CustomComponent {

		private static final long serialVersionUID = 1L;

		public DetailsButton(final Auction a) {
			Button button = new Button("details");

			button.setData(a);
			button.addClickListener(new ClickListener() {

				private static final long serialVersionUID = 1L;

//				@Override
				public void buttonClick(ClickEvent event) {
					FastsellUI.navigateTo(AuctionDetailView.NAME + "/" + a.getAuctionID());
				}
			});

			this.setCompositionRoot(button);
		}

	}

}
